//
//  FriendViewController.m
//  soccer
//
//  Created by 上官千城 on 15/9/2.
//  Copyright (c) 2015年 上官千城. All rights reserved.
//

#import "OFriendViewController.h"
#import "ContactsViewController.h"
#import "ChatListViewController.h"
#import "AddFriendViewController.h"
@interface OFriendViewController ()

@property(nonatomic, strong)ChatListViewController *chatListVC;
@property(nonatomic, strong)ContactsViewController *contactsVC;

@end

@implementation OFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor cyanColor];
    
    self.getUserInfo = [[OGetUserInfo alloc] initWithCallBack:@selector(callBack) for:self];
    
    //添加好友按钮(右侧)
    self.addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.addButton addTarget:self action:@selector(addActionView) forControlEvents:UIControlEventTouchUpInside];
    self.addButton.frame = CGRectMake(0, 0, 30, 30);
    [self.addButton setTitle:@"+" forState:UIControlStateNormal];
    self.addButton.titleLabel.font = [UIFont systemFontOfSize:26];
    self.addButton.tintColor = [UIColor whiteColor];
    self.right = [[UIBarButtonItem alloc]initWithCustomView:self.addButton];
    self.navigationItem.rightBarButtonItem = self.right;
    
    
    
    //容器视图控制器  子视图好友列表
    //    self.listView = [[FriendListController alloc]init];
    //    [self addChildViewController:self.listView];
    //    [self.view addSubview:self.listView.view];
    self.contactsVC = [[ContactsViewController alloc] init];
    [self addChildViewController:self.contactsVC];
    [self.view addSubview:self.contactsVC.view];
    
    //容器视图控制器  子视图最近联系人
    //    self.recently = [[RecentlyFriendController alloc]init];
    //    [self addChildViewController:self.recently];
    //    [self.view addSubview:self.recently.view];
    self.chatListVC = [[ChatListViewController alloc] init];
    [self addChildViewController:self.chatListVC];
    [self.view addSubview:self.chatListVC.view];
    
    
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"最近联系",@"好友列表", nil];
    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:arr];
    segment.selectedSegmentIndex = 0;
    segment.frame = CGRectMake(0, 0, 120, 30);
    self.navigationItem.titleView = segment;
    [segment addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];
    self.open = NO;
    //    [self creatSearchBar];
    [self addaView];
}

//-(void)creatSearchBar
//{
//    self.mySearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 50)];
//    self.mySearchBar.delegate = self;
//    self.mySearchBar.placeholder = @"请输入搜索内容";
//    //首字母是否大写
//    [self.mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
//    [self.view addSubview:self.mySearchBar];
////    self.tableView.tableHeaderView = self.mySearchBar;
//
//    self.mySearchDisplayController = [[UISearchDisplayController alloc]initWithSearchBar:self.mySearchBar contentsController:self];
//    self.mySearchDisplayController.delegate = self;
//    self.mySearchDisplayController.searchResultsDelegate = self;
//    self.mySearchDisplayController.searchResultsDataSource = self;
//
//    //去掉底部多余的表格线
//    [self.mySearchDisplayController.searchResultsTableView setTableFooterView:[[UIView alloc]initWithFrame:CGRectZero]];
//}
//搜索改变"取消"字
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    self.mySearchBar.showsCancelButton = YES;
    NSArray *subViews;
    subViews = [(self.mySearchBar.subviews[0]) subviews];
    for (id view in subViews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton* cancelbutton = (UIButton* )view;
            [cancelbutton setTitle:@"取消" forState:UIControlStateNormal];
            break;
        }
    }
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString scope:[self.mySearchBar scopeButtonTitles][self.mySearchBar.selectedScopeButtonIndex] ];
    return YES;
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    [self filterContentForSearchText:self.mySearchBar.text scope:self.mySearchBar.scopeButtonTitles[searchOption]];
    return YES;
}



-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSMutableArray *tempResults = [NSMutableArray array ];
    //NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较，区分大小写 NSNumericSearch:比较字符串的字符个数，而不是字符值。NSDiacriticInsensitiveSearch忽略变音符号
    
    NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;
    
    for (int i = 0; i < self.dataArray.count; i++) {
        
        NSString *storeString = self.dataArray[i];
        NSRange storeRange = NSMakeRange(0, storeString.length);
        NSRange foundRange = [storeString rangeOfString:searchText options:searchOptions range:storeRange];
        if (foundRange.length) {
            [tempResults addObject:storeString];
        }
        
    }
    [self.resultsData removeAllObjects];
    [self.resultsData addObjectsFromArray:tempResults];
    //    NSLog(@"%@",self.resultsData);
}




-(void)addActionView
{
    
    if (self.open == NO) {
        [UIView animateWithDuration:0.1 animations:^{
            self.addView.frame = CGRectMake(0, 0, self.addView.frame.size.width, self.addView.frame.size.height);
            
        }];
        NSLog(@"添加好友");
        
        self.open = YES;
    } else {
        [UIView animateWithDuration:0.1 animations:^{
            self.addView.frame = CGRectMake(WIDTH, 0, self.addView.frame.size.width, self.addView.frame.size.height);
            
        }];
        self.open = NO;
    }
    
    
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = NO;
    
    [self closeAction];
    
    
}
-(void)addaView
{
    
    //底层View
    self.addView = [[UIView alloc]initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT)];
    self.addView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.addView];
    //底层view 轻拍手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeAction)];
    [self.addView addGestureRecognizer:tap];
    //弹出列表
    UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(WIDTH-90,0 , 90, 90)];
    aView.backgroundColor = [UIColor blackColor];
    [self.addView addSubview:aView];
    
    
    UIButton *addF = [UIButton buttonWithType:UIButtonTypeCustom];
    addF.frame = CGRectMake(0, 0, aView.frame.size.width, 29);
    [addF addTarget:self action:@selector(addfriend) forControlEvents:UIControlEventTouchUpInside];
    [addF setTitle:@"添加好友" forState:UIControlStateNormal];
    [addF setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [aView addSubview:addF];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 30, self.addView.frame.size.width, 1)];
    line.backgroundColor = [UIColor whiteColor];
    [aView addSubview:line];
    //添加组
    UIButton *addG = [UIButton buttonWithType:UIButtonTypeCustom];
    addG.frame = CGRectMake(0, 30, aView.frame.size.width, 29);
    [addG setTitle:@"创建群组" forState:UIControlStateNormal];
    [addG setTintColor:[UIColor whiteColor]];
    [addG addTarget:self action:@selector(addgroup) forControlEvents:UIControlEventTouchUpInside];
    [aView addSubview:addG];
    
    UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(0, addG.frame.origin.y + addG.frame.size.height, aView.frame.size.width, 1)];
    line1.backgroundColor = [UIColor whiteColor];
    [aView addSubview:line1];
    //二维码
    UIButton *QR = [UIButton buttonWithType:UIButtonTypeCustom];
    QR.frame = CGRectMake(0, 60, aView.frame.size.width, 30);
    [QR setTitle:@"扫一扫" forState:UIControlStateNormal];
    [QR setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [QR addTarget:self action:@selector(QRscan) forControlEvents:UIControlEventTouchUpInside];
    [aView addSubview:QR];
}

//扫描二维码
-(void)QRscan
{
    
    QRCodeReaderViewController *reader = [QRCodeReaderViewController new];\
    reader.modalPresentationStyle = UIModalPresentationFormSheet;
    reader.delegate = self;
    
    __weak typeof (self) wSelf = self;
    [reader setCompletionWithBlock:^(NSString *resultAsString) {
        [wSelf.navigationController popViewControllerAnimated:YES];

    }];
    
    reader.hidesBottomBarWhenPushed = YES;

    //[self presentViewController:reader animated:YES completion:NULL];
    [self.navigationController pushViewController:reader animated:YES];
    
  
    
}

#pragma mark - QRCodeReader Delegate Methods
//获取扫描二维码信息
- (void)reader:(QRCodeReaderViewController *)reader didScanResult:(NSString *)result
{
    
    [self getUserInfor:result];
    
 
}

-(void)getUserInfor:(NSString *)str
{
    
//    self.getUserInfo.userId = str;
    self.getUserInfo.userId = @"218";
    [self.getUserInfo request];


}

//用户信息请求响应信息
-(void)callBack
{

    NSLog(@"userInfo == %@",self.getUserInfo.nickName);

    OPerson *per = [[OPerson alloc] init];
    [per setValuesForKeysWithDictionary:self.getUserInfo.userInfor];
    OPersonViewController *person = [[OPersonViewController alloc] init];
    OBaseNavViewController *nav = [[OBaseNavViewController alloc] initWithRootViewController:person];
    
    
    person.per = per;
    
    [self presentViewController:nav animated:YES completion:^{
        
        
    }];


}

- (void)readerDidCancel:(QRCodeReaderViewController *)reader
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)closeAction
{
    [UIView animateWithDuration:0.1 animations:^{
        self.addView.frame = CGRectMake(WIDTH, 0, self.addView.frame.size.width, self.addView.frame.size.height);
        
    }];
    self.open = NO;
}

-(void)addgroup
{
    //    NSLog(@"添加群");
    //    addGroupController *view = [[addGroupController alloc]init];
    //    [self.navigationController pushViewController:view animated:YES];
}


-(void)addfriend
{
    NSLog(@"添加好友");
    //    addFrendController *view = [[addFrendController alloc]init];
    //    [self.navigationController pushViewController:view animated:YES];
    [self.navigationController pushViewController:[[AddFriendViewController alloc] init] animated:YES];
    [self closeAction];
    
}





//容器视图控制器调换
- (void)segmentedControlAction:(UISegmentedControl *)Seg
{
    NSInteger index = Seg.selectedSegmentIndex;
    switch (index) {
        case 0:
            [self.view bringSubviewToFront:self.chatListVC.view];
            
            [self.view bringSubviewToFront:self.addView];
            break;
        case 1:
            [self.view bringSubviewToFront:self.contactsVC.view];
            
            [self.view bringSubviewToFront:self.addView];
            break;
            
        default:
            break;
    }
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
